<template>
  <el-dialog
    :title="!dataForm.coId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品名" prop="coName">
      <el-input v-model="dataForm.coName" placeholder="商品名"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="coPrice">
      <el-input v-model="dataForm.coPrice" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="coImg">
      <el-input v-model="dataForm.coImg" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="商品所属分类" prop="coClId">
      <el-input v-model="dataForm.coClId" placeholder="商品所属分类"></el-input>
    </el-form-item>
    <el-form-item label="店铺id" prop="coBuId">
      <el-input v-model="dataForm.coBuId" placeholder="店铺id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          coId: 0,
          coName: '',
          coPrice: '',
          coImg: '',
          coClId: '',
          coBuId: ''
        },
        dataRule: {
          coName: [
            { required: true, message: '商品名不能为空', trigger: 'blur' }
          ],
          coPrice: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          coImg: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          coClId: [
            { required: true, message: '商品所属分类不能为空', trigger: 'blur' }
          ],
          coBuId: [
            { required: true, message: '店铺id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.coId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.coId) {
            this.$http({
              url: this.$http.adornUrl(`/sulingpxqg/commodity/info/${this.dataForm.coId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.coName = data.commodity.coName
                this.dataForm.coPrice = data.commodity.coPrice
                this.dataForm.coImg = data.commodity.coImg
                this.dataForm.coClId = data.commodity.coClId
                this.dataForm.coBuId = data.commodity.coBuId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sulingpxqg/commodity/${!this.dataForm.coId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'coId': this.dataForm.coId || undefined,
                'coName': this.dataForm.coName,
                'coPrice': this.dataForm.coPrice,
                'coImg': this.dataForm.coImg,
                'coClId': this.dataForm.coClId,
                'coBuId': this.dataForm.coBuId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
